package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Friendship;
import cn.wolfcode.trip.base.domain.FriendshipComment;
import cn.wolfcode.trip.base.domain.FriendshipPraise;
import cn.wolfcode.trip.base.query.FriendshipQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IFriendshipCommentService;
import cn.wolfcode.trip.base.service.IFriendshipService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 朋友圈资源控制器
 */
@RestController
@RequestMapping("/friendships")
public class FriendshipController {

    @Autowired
    private IFriendshipService friendshipService;
    @Autowired
    private IFriendshipCommentService commentService;

    /**
     * 列出当条朋友圈
     */
    @GetMapping("/{friendshipId}")
    public PageInfo<Friendship> getFriendship(@PathVariable Long friendshipId){

        return  friendshipService.getFirendshipById(friendshipId);
    }


    /**
     * 列出朋友圈
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo<Friendship> query(FriendshipQueryObject qo){
        //qo.setOrderBy("fs.createTime desc");
        PageInfo<Friendship> query = friendshipService.query(qo);
        return query;
    }

    /**
     * 保存朋友圈的一级评论
     */
    @PostMapping("/{friendship.id}/comments")
    public JSONResult saveComment(FriendshipComment comment){
        JSONResult jsonResult = new JSONResult();
        try {
            FriendshipComment saveComment = commentService.saveComment(comment);
            jsonResult.setComment(saveComment);
        } catch (Exception e) {
            jsonResult.mark("保存失败,稍后再试");
            e.printStackTrace();
        }
        return jsonResult;
    }

    /**
     * lituan
     * 新增朋友圈
     * @param friendship
     * @return
     */
    @PostMapping
    public JSONResult saveFriendship(Friendship friendship){
        JSONResult jsonResult = new JSONResult();
        try {
            friendshipService.saveFriendship(friendship);
        } catch (Exception e) {
            jsonResult.mark("保存失败,稍后再试");
            e.printStackTrace();
        }
        return jsonResult;
    }

    /**
     * 新增点赞
     * @param praise
     */
    @PostMapping("/{friendship.id}/praise")
    public JSONResult saveGood(FriendshipPraise praise){
        JSONResult jsonResult=new JSONResult();
        try {
            friendshipService.saveGood(praise);
        } catch (Exception e) {
            jsonResult.mark("");
            e.printStackTrace();
        }
        return jsonResult;
    }

    /**
     * 删除点赞
     */
    @DeleteMapping("/{friendshipId}/praise")
    public void deleteGood(@PathVariable Long friendshipId){
        friendshipService.deleteGood(friendshipId);
    }

    /**
     * 查是否有点赞
     */
    @GetMapping("/{friendshipId}/praise")
    public Object getGood(@PathVariable Long friendshipId){
        Object good = friendshipService.getGood(friendshipId);
        if (good != null) {
            return good;
        }
        return null;
    }
}
